package com.laoye.controller;

import com.laoye.bean.Order;
import com.laoye.bean.Product_Order;
import com.laoye.constant.AdminConstent;
import com.laoye.dto.OrderCreateDTO;
import com.laoye.dto.OrderDetail;
import com.laoye.dto.OrderEchartsDTO;
import com.laoye.service.OrderService;
import com.laoye.util.EmailUtil;
import com.laoye.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderService orderService;
    /**
     * 获取订单状态统计数据
     * @return 订单统计数据
     */
    @GetMapping("/getOrderCount")
    public Result<List<OrderEchartsDTO>> getOrderCount(){
        return Result.success(orderService.getOrderCount());
    }
    @PutMapping("/completeOrder/{id}")
    public Result<String> completeOrder(@PathVariable("id") Integer id){
        orderService.completeOrder(id);
        return Result.success("订单完成");
    }
    @GetMapping("/getUserOrderList/{status}")
    public Result<List<OrderDetail>> getUserOrderList(@PathVariable("status") Byte status){
        return Result.success(orderService.getUserOrderList(status));
    }
    @PutMapping("deleteOrder/{id}")
    public Result<String> deleteOrder(@PathVariable("id") Integer id){
        orderService.deleteOrder(id);
        return Result.success("订单删除成功");
    }

    @PutMapping("updateDeliveryTime/{id}")
    public Result<String> updateDeliveryTime(@PathVariable("id") Integer id){
        orderService.updateDeliveryTime(id);
        return Result.success("发货成功");
    }

    @GetMapping("/getOrderList/{status}")
    public Result<List<OrderDetail>> getOrderList(@PathVariable("status") byte status){
        List<OrderDetail> orderList = orderService.getOrderList(status);
        return Result.success( orderList);
    }


    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody OrderCreateDTO order){
        orderService.addOrder(order);
        System.out.println(order.getOrder().getId());
        return Result.success("正在生成订单", order.getOrder().getId());
    }

    @GetMapping("/getOrderById")
    public Result<Order> getOrderById(@RequestParam("id") Integer id){
        return Result.success(orderService.getOrderById(id));
    }
    @GetMapping("/getProductById/{id}")
    public Result<List<Product_Order>> getProductById(@PathVariable("id") Integer id){
        return Result.success(orderService.getProductById(id));
    }

    @PutMapping("/updateSetPayTime")
    public Result<String> updateSetPayTime(@RequestParam("id") Integer id){
        orderService.updateSetPayTime(id);
        EmailUtil.sendEmail(AdminConstent.ADMIN_EMAIL,"管理员先生您好，您有新的订单等待处理");
        return Result.success("");
    }
}
